from Foo.capability_json import getDriver
from Foo.login import Login
from Foo.pageSwitch import PageSwitch
from util.baseView import BaseView
from selenium.webdriver.common.by import By
from appium.webdriver.common.touch_action import TouchAction
import logging
import logging.config
from faker import Faker

import time


class Person(BaseView):
    CON_LOG = '../docs/log.conf'
    logging.config.fileConfig(CON_LOG)
    f = Faker(locale='zh_CN')

    def __init__(self, driver):
        BaseView.__init__(self, driver)
        self.image_icon = (By.ID, 'com.sykean.biometrics:id/imageView_face_icon')
        self.edit = (By.ID, 'com.sykean.biometrics:id/modify')  # 人员编辑按钮
        self.iris_capture = (By.ID, 'com.sykean.biometrics:id/btn_iris_capture')  # 虹膜采集
        self.face_capture = (By.ID, 'com.sykean.biometrics:id/btn_face_capture')
        self.dialog_ok = (By.ID, 'com.sykean.biometrics:id/btn_dialog_ok')  # 确认是否重新采集虹膜
        self.dialog_cancel = (By.ID, 'com.sykean.biometrics:id/btn_dialog_cancel')  # 确认是否取消本次操作
        self.back = (By.ID, 'com.sykean.biometrics:id/btn_back')
        self.add_user = (By.ID,'com.sykean.biometrics:id/ibtn_add_user')
        # self.edit_values = (By.ID,'com.sykean.biometrics:id/edt_edit_value')
        self.save_user = (By.ID,'com.sykean.biometrics:id/btn_save_user')
    def test_edit_person(self):
        el = self.find_element(*self.image_icon)
        # 长按
        TouchAction(self.driver).long_press(el).perform()
        time.sleep(2)
        self.click_element(*self.edit)
    def test_add_user(self):
        # 人员新增按钮
        self.click_element(*self.add_user)

        eles = self.driver.find_elements_by_class_name("android.widget.EditText")
        # 警号
        eles[0].send_keys(self.f.random_int())
        # 姓名
        eles[1].send_keys(self.f.name())
        # 身份证号
        eles[2].send_keys(self.f.ssn())
        # 卡号
        # eles[3].send_keys(self.f.phone_number())
        # 部门
        eles[4].send_keys(self.f.company_prefix())
        self.click_element(*self.save_user)

    def test_back(self):
        self.click_element(*self.back)


if __name__ == '__main__':
    driver = getDriver()
    Login(driver).login()
    PageSwitch(driver).menuUser()
    count = 0
    while True:
        print('-----------开始新增第{0}个人--------------'.format(count+1))
        try:
            Person(driver).test_add_user()
            count += 1
        except:
            print('异常终止，请继续')
            Person(driver).test_back()





